package com.ddwl.goods.dao;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.Constant;
import com.ddwl.common.dao.TkDao;
import com.ddwl.goods.mapper.DiscountItemMapper;
import com.ddwl.goods.model.DiscountItem;
import com.ddwl.schema.bo.goods.retail.DiscountItemChangeStockBo;
import com.ddwl.schema.bo.goods.retail.DiscountItemPageBo;
import com.ddwl.schema.bo.goods.retail.DiscountItemSaleBo;
import com.ddwl.schema.vo.goods.retail.DiscountItemPageVo;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.List;

/**
 * @Author hemiaojun
 * @Date 2021/3/27 16:32
 */
@Repository
public class DiscountItemDao extends TkDao<DiscountItemMapper, DiscountItem> {

    @Resource
    private DiscountItemMapper discountItemMapper;

    public List<DiscountItemPageVo> getDiscountItemList(DiscountItemPageBo param) {
        return discountItemMapper.getDiscountItemList(param);
    }

    public List<DiscountItem> selectByItemIds(List<String> list) {
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        Example example = getExample();
        example.and().andIn("itemId", list)
                .andEqualTo("status", BaseEnum.StatusEnum.ENABLE.getCode());
        return discountItemMapper.selectByExample(example);
    }

    public void batchUpdateStock(List<DiscountItemChangeStockBo> boList) {
        discountItemMapper.batchUpdateStock(boList);
    }

    public List<DiscountItem> getDuplicationDiscountItem(String itemId,String id) {
        Example example = getExample();
        example.and().andEqualTo("itemId", itemId)
                .andEqualTo("status", BaseEnum.StatusEnum.ENABLE.getCode())
                .andNotEqualTo("id",id);
        return discountItemMapper.selectByExample(example);
    }

    public List<DiscountItem> getListByItemIds(List<String> itemIds) {
        if (CollectionUtils.isEmpty(itemIds)) {
            return null;
        }
        Example example = getExample();
        example.and().andIn("itemId", itemIds)
                .andEqualTo("status", BaseEnum.StatusEnum.ENABLE.getCode());
        return discountItemMapper.selectByExample(example);
    }

    public DiscountItem getByItemId(String id) {
        DiscountItem discountItem = new DiscountItem();
        discountItem.setItemId(id);
        discountItem.setStatus(BaseEnum.StatusEnum.ENABLE.getCode());
        return this.selectOne(discountItem);
    }

    public void deleteByItemId(String itemId) {
        Example example = getExample();
        example.and().andEqualTo("itemId", itemId);
        DiscountItem discountItem = new DiscountItem();
        discountItem.setDflag(BaseEnum.DflagEnum.DELETED.getCode());
        discountItem.setGmtModified(Instant.now());
        discountItemMapper.updateByExampleSelective(discountItem,example);
    }

    public void updateStatus(String itemId, Integer status) {
        Example example = getExample();
        example.and().andEqualTo("itemId", itemId);
        DiscountItem discountItem = new DiscountItem();
        discountItem.setStatus(status);
        discountItemMapper.updateByExampleSelective(discountItem,example);
    }

    public void updateSale(List<DiscountItemSaleBo> list) {
        if (!CollectionUtils.isEmpty(list)) {
            discountItemMapper.updateSale(list);
        }
    }
}
